package cn.zooz.common.util.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Bradish7Y on 2017/8/14.
 */
@Aspect
public class DaoTimeCostAspect {

    private static Logger _log = LoggerFactory.getLogger(DaoTimeCostAspect.class);

    private static final String POINT_CUT = "execution(* com.xing.dao..*.*(..))";

    @Before(POINT_CUT)
    public void doBeforeInServiceLayer(JoinPoint joinPoint) {

    }

    @After(POINT_CUT)
    public void doAfterInServiceLayer(JoinPoint joinPoint) {

    }

    @Around(POINT_CUT)
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        //执行
        Object result = pjp.proceed();

        Signature signature = pjp.getSignature();
        String method = signature.getDeclaringTypeName() + "#" + signature.getName();

        if (startTime > 1) {
            _log.warn("method:{}, time elapsed:{}ms", method, System.currentTimeMillis() - startTime);
        }

        return result;
    }
}
